package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.common.utils.Common;
import com.think.teaching.mapper.CourseUserMapper;
import com.think.teaching.model.Course;
import com.think.teaching.model.CourseUser;
import com.think.teaching.service.CourseUserService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户课程 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class CourseUserServiceImpl extends BaseServiceImpl<CourseUserMapper, CourseUser> implements CourseUserService {

	@Override
	public CourseUser AddCourseUser(CourseUser courseUser) {
		if (this.save(courseUser)) {
			return courseUser;
		}
		return null;
	}


	@Override
	public CourseUser UpdateCourseUser(CourseUser courseUser) {
		if (this.updateById(courseUser)) {
			return courseUser;
		}
		return null;
	}

	@Override
	public CourseUser GetCourseUser(Integer id) {
		QueryWrapper<CourseUser> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	@Override
	public List<CourseUser> SearchCourseUserWithKey(String key,Boolean withoutBan) {
		QueryWrapper<CourseUser> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("content", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return list(queryWrapper);
	}

	public IPage<Map<String, Object>> ListCourseUser(Integer count, Integer page, Integer course) {
		IPage<Map<String, Object>> ipage = new Page<Map<String,Object>>(page,count);
		return baseMapper.ListCourseUser(ipage, course);
	}
	
	public IPage<Course> ListUserSelectCourse(Integer count, Integer page) {
		IPage<Course> ipage = new Page<>(page, count);
		return baseMapper.ListCourseUserWithTagAndType(ipage, Common.GetCurrentAuthor().getUserId());
	}

	@Override
	public CourseUser SetCourseUserState(Integer id, Integer state) {
		UpdateWrapper<CourseUser> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}
	
	public CourseUser GetByUserIDAndCourseId(Integer courseId,Integer userId) {
		QueryWrapper<CourseUser> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("course_id", courseId).eq("student_id", userId).ne("delflag", 1);
		return getOne(queryWrapper);
	}
	


}
